home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / XML Utilities / Professional Programmer XSL IDE / Xselerator25.msi / Data.Cab / F22794_Compare.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2001-09-24  |  6.0 KB  |  148 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output method="text" encoding="ISO-8859-1"/>
  4. <!-- This stylesheet to be run against the 'source' xml document -->
  5. <!-- e.g. BooksA.xml -->
  6.  
  7. <!-- can pass the other file by param -->
  8. <xsl:param name="compare-file-name" select="'booksB.xml'"/>
  9.  
  10. <!-- setup vars to access the two docs regardless of context -->
  11. <xsl:variable name="second-src" select="document($compare-file-name)"/>
  12. <xsl:variable name="primary-src" select="/"/>
  13.  
  14. <xsl:template match="/">
  15.     <xsl:apply-templates select="books/book" mode="main"/>
  16.     <!-- only thing left to check now is for Book -->
  17.     <!-- elements in compare that are not present -->
  18.     <!-- in the source doc                        -->
  19.     <xsl:apply-templates select="($second-src)/books/book" mode="source-missing"/>
  20. </xsl:template>
  21.  
  22. <!-- ============================================== -->
  23. <!-- main applied template for comparing the book   -->
  24. <!-- elements between the two documents             -->
  25. <xsl:template match="book" mode="main">
  26.     <!-- look for the same book (i.e. with same isbn) in other document -->
  27.     <xsl:variable name="other" select="($second-src)/books/book[@isbn = current()/@isbn]"/>
  28.     <xsl:variable name="differences">
  29.         <xsl:choose>
  30.             <xsl:when test="count($other) = 0">
  31.                 <!-- no matching book element in the other document -->
  32.                 <xsl:text><book> element does not exist in comparison document </xsl:text>
  33.             </xsl:when>
  34.             <xsl:otherwise>
  35.                 <!-- compare A with B -->
  36.                 <xsl:apply-templates select="*">
  37.                     <xsl:with-param name="other" select="$other"/>
  38.                     <xsl:with-param name="other-file" select="'comparison document'"/>
  39.                 </xsl:apply-templates>
  40.                 <!-- and compare the other way (i.e. B with A) -->
  41.                 <xsl:apply-templates select="$other/*">
  42.                     <xsl:with-param name="other" select="."/>
  43.                     <xsl:with-param name="other-file" select="'source document'"/>
  44.                     <xsl:with-param name="is-other" select="true()"/>
  45.                 </xsl:apply-templates>
  46.             </xsl:otherwise>
  47.         </xsl:choose>
  48.     </xsl:variable>
  49.     <xsl:if test="string-length($differences)">
  50.         <xsl:text>Comparing Book ISBN #</xsl:text>
  51.         <xsl:value-of select="@isbn"/><xsl:text> </xsl:text>
  52.         <xsl:value-of select="$differences"/>
  53.         <xsl:text>====================== </xsl:text>
  54.     </xsl:if>
  55. </xsl:template>
  56.  
  57. <!-- ========================================== -->
  58. <!-- template to be applied to find differences -->
  59. <!-- between child elements of the book         -->
  60. <xsl:template match="book//*">
  61.     <xsl:param name="other"/>
  62.     <xsl:param name="other-file"/>
  63.     <xsl:param name="is-other" select="false()"/>
  64.     <xsl:variable name="compare-node" select="($other)/*[name() = name(current())]"/>
  65.     <xsl:choose>
  66.         <xsl:when test="count($compare-node) = 0">
  67.             <!-- element doesn't exist in the comparison -->
  68.             <xsl:text><</xsl:text>
  69.             <xsl:value-of select="name()"/>
  70.             <xsl:text>> element does not exist in </xsl:text>
  71.             <xsl:value-of select="$other-file"/>
  72.             <xsl:text> </xsl:text>
  73.         </xsl:when>
  74.         <xsl:otherwise>
  75.             <!-- check if element text is the same or diff -->
  76.             <!-- NB. only compare one way otherwise the -->
  77.             <!--     diff is output twice!              -->
  78.             <xsl:if test="not($is-other) and (./text() != $compare-node/text())">
  79.                 <xsl:text><</xsl:text>
  80.                 <xsl:value-of select="name()"/>
  81.                 <xsl:text>> value is different </xsl:text>
  82.             </xsl:if>
  83.             <!-- do comparison of atts for this element -->
  84.             <xsl:apply-templates select="@*">
  85.                 <xsl:with-param name="other" select="$compare-node"/>
  86.                 <xsl:with-param name="other-file" select="$other-file"/>
  87.                 <xsl:with-param name="is-other" select="$is-other"/>
  88.             </xsl:apply-templates>
  89.             <!-- do comparison of child elements for this element -->
  90.             <xsl:apply-templates select="*">
  91.                 <xsl:with-param name="other" select="$compare-node/*"/>
  92.                 <xsl:with-param name="other-file" select="$other-file"/>
  93.                 <xsl:with-param name="is-other" select="$is-other"/>
  94.             </xsl:apply-templates>
  95.         </xsl:otherwise>
  96.     </xsl:choose>
  97. </xsl:template>
  98.  
  99. <!-- ============================================= -->
  100. <!-- template to be applied to find differences    -->
  101. <!-- between attributes of the descendants of book -->
  102. <xsl:template match="book//@*">
  103.     <xsl:param name="other"/>
  104.     <xsl:param name="other-file"/>
  105.     <xsl:param name="is-other" select="false()"/>
  106.     <xsl:variable name="compare-att" select="($other)/@*[name() = name(current())]"/>
  107.     <xsl:choose>
  108.         <xsl:when test="count($compare-att) = 0">
  109.             <!-- attribute doesn't exist in the comparison -->
  110.             <xsl:text><</xsl:text>
  111.             <xsl:value-of select="name(..)"/>
  112.             <xsl:text>></xsl:text>
  113.             <xsl:text> attribute @</xsl:text>
  114.             <xsl:value-of select="name()"/>
  115.             <xsl:text> does not exist </xsl:text>
  116.             <xsl:value-of select="$other-file"/>
  117.             <xsl:text> </xsl:text>
  118.         </xsl:when>
  119.         <!-- NB. only compare one way otherwise the -->
  120.         <!--     diff is output twice!              -->
  121.         <xsl:when test="not($is-other) and (. != $compare-att)">
  122.             <!-- values of atts are different -->
  123.             <xsl:text><</xsl:text>
  124.             <xsl:value-of select="name(..)"/>
  125.             <xsl:text>></xsl:text>
  126.             <xsl:text> attribute @</xsl:text>
  127.             <xsl:value-of select="name()"/>
  128.             <xsl:text> value is different  </xsl:text>
  129.         </xsl:when>
  130.     </xsl:choose>
  131. </xsl:template>
  132.  
  133. <!-- ============================================== -->
  134. <!-- this template just looks for books that appear -->
  135. <!-- in the compare doc but not in the source doc   -->
  136. <xsl:template match="book" mode="source-missing">
  137.     <xsl:variable name="primary" select="($primary-src)/books/book[@isbn = current()/@isbn]"/>
  138.     <xsl:if test="count($primary) = 0">
  139.         <xsl:text>Comparing Book ISBN #</xsl:text>
  140.         <xsl:value-of select="@isbn"/>
  141.         <xsl:text> </xsl:text>
  142.         <xsl:text><book> element does not exist in source document </xsl:text>
  143.         <xsl:text>====================== </xsl:text>
  144.     </xsl:if>
  145. </xsl:template>
  146.  
  147. </xsl:stylesheet>
  148.